/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with 
 * this distribution in the License.txt file.
 */

package jdbcmanager.swing.desktop;

import java.awt.event.*;
import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.swing.JxInternalFrame;

/**
 * An extension of JDesktopPane that enhances JDesktopPane providing:
 * <p>
 * <ul>
 *   <li>Support for standard MDI functionality: tile, cascade, close-all, etc.
 *   <li>Virtual desktop: scroll-bars when windows are moved outside of view port.
 * </ul>
 *
 * @author Francisco Morero Peyrona
 */

public final class JxDesktopPane extends JDesktopPane implements ActionListener
{
   /**
    * Class constructor
    */
   public JxDesktopPane()
   {
      setDesktopManager( new JxDesktopManager( this ) );
      
      addMouseListener( new MouseAdapter()
      {
         public void mousePressed( MouseEvent evt ) 
         {
            if( evt.isPopupTrigger() )
               showPopup( JxDesktopPane.this, evt.getX(), evt.getY() );
         }
         
         public void mouseReleased( MouseEvent evt ) 
         {
            if( evt.isPopupTrigger() )
               showPopup( JxDesktopPane.this, evt.getX(), evt.getY() );
         }
      } );
   }
   
   public void showPopup( JComponent invoker, int x, int y )
   {
      JInternalFrame[] aIF     = getAllFrames();
      boolean          bFrames = aIF.length > 0;
         
      JPopupMenu menu = new JPopupMenu();
      
      JMenuItem  miTile     = new JMenuItem( "Tile" );
                 miTile.setActionCommand( "TILE" );
                 miTile.addActionListener( this );
                 miTile.setEnabled( bFrames );
      JMenuItem  miMosaic   = new JMenuItem( "Mosaic" );
                 miMosaic.setActionCommand( "MOSAIC" );
                 miMosaic.addActionListener( this );
                 miMosaic.setEnabled( bFrames );
      JMenuItem  miCascade  = new JMenuItem( "Cascade" );
                 miCascade.setActionCommand( "CASCADE" );
                 miCascade.addActionListener( this );
                 miCascade.setEnabled( bFrames );
      JMenuItem  miMinimize = new JMenuItem( "Minimize all" );
                 miMinimize.setActionCommand( "MINIMIZE" );
                 miMinimize.addActionListener( this );
                 miMinimize.setEnabled( bFrames );
      JMenuItem  mnuArrange  = new JMenuItem( "Arrange Icons" );
                 mnuArrange.setActionCommand( "ARRANGE" );
                 mnuArrange.addActionListener( this );
                 mnuArrange.setEnabled( bFrames );
      JMenuItem  miCloseAll = new JMenuItem( "Close all" );
                 miCloseAll.setActionCommand( "CLOSE_ALL" );
                 miCloseAll.addActionListener( this );
                 miCloseAll.setEnabled( bFrames );
      
      //--------------------------------------------------------//
      // Creates a sub menu with all JInternalFrame titles
      JMenu mnuWnd  = new JMenu( "Frames" );
            mnuWnd.setEnabled( bFrames );
      
      for( int n = 0; n < aIF.length; n++ )
      {
         JMenuItem mi = new JMenuItem( aIF[n].getTitle() );
                   mi.putClientProperty( "FRAME", aIF[n] );
         mi.addActionListener( this );
      
         mnuWnd.add( mi );
      }
      //-------------------------------------------------------//
         
      JMenuItem  miAppConfig = new JMenuItem( "Configuration ..." );
                 miAppConfig.setActionCommand( "APP_CONFIG" );
                 miAppConfig.addActionListener( this );
         
      menu.add( mnuWnd      );
      menu.addSeparator();
      menu.add( miTile      );
      menu.add( miMosaic    );
      menu.add( miCascade   );
      menu.add( miMinimize  );
      menu.add( mnuArrange  );
      menu.add( miCloseAll  );
      menu.addSeparator();
      menu.add( miAppConfig );
      
      menu.show( invoker, x, y );
   }
   
   /**
    * 
    * @param evt
    */
   public void actionPerformed( ActionEvent evt )
   {
      JMenuItem item    = (JMenuItem) evt.getSource();
      String    sAction = item.getActionCommand();

      if(      sAction.equals( "TILE"       ) ) tile();
      else if( sAction.equals( "MOSAIC"     ) ) mosaic();
      else if( sAction.equals( "CASCADE"    ) ) cascade();
      else if( sAction.equals( "MINIMIZE"   ) ) minimizeAll();
      else if( sAction.equals( "ARRANGE"    ) ) arrangeIcons();
      else if( sAction.equals( "CLOSE_ALL"  ) ) closeAll();
      else if( sAction.equals( "APP_CONFIG" ) ) App.showAppConfigDialog();
      else                                      setActiveFrame( (JInternalFrame) item.getClientProperty( "FRAME" ) );
   }

   //-------------------------------------------------------------------------//
   // FRAME SELECTION METHODS
   //-------------------------------------------------------------------------//

   /**
    * @param frm The Internal Frame to became selected
    */
   public void setActiveFrame( JInternalFrame frm )
   {
      ((JxInternalFrame) frm).restore();
      moveToFront( frm );
      ((JxInternalFrame) frm).setSelected( true );
      scrollRectToVisible( frm.getBounds() );
   }
   
   public void checkSize()
   {
      ((JxDesktopManager) getDesktopManager()).resizeDesktop();
   }
   
   //-------------------------------------------------------------------------//
   // ARRANGING FRAMES AND ICONS METHODS
   //-------------------------------------------------------------------------//

   public void tile()
   {
      JxDesktopUtils.tile( this );
   }

   public void mosaic()
   {
      JxDesktopUtils.mosaic( this );
   }

   public void cascade()
   {
      JxDesktopUtils.cascadeAll( this );
   }

   public void minimizeAll()
   {
      JxDesktopUtils.minimizeAll( this );
   }

   public void arrangeIcons()
   {
      JxDesktopUtils.arrangeIcons( this );
   }
   
   public void closeAll()
   {
      JxDesktopUtils.closeAll( this );
   }
}